/*
    Copyright 2009 Michael Ogawa
 
 This file is part of Wordookie.
 
 Wordookie is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Wordookie is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with Wordookie.  If not, see <http://www.gnu.org/licenses/>.
 */

import wordookie.*;
import wordookie.util.ColorStuff;
import wordookie.parsers.DumbParser;

final String INPUT_FILE = "TotalEclipse.txt";
final String FONT_NAME = "Serif";
final color BACKGROUND = color( 0 );
final int MAX_FONTSIZE = 100;
final int MIN_FONTSIZE = 16;
final int FADE = 2;
final boolean TAKE_SNAPSHOTS = false;

java.util.List stringList;
Iterator itr;
Map wordMap;
Layout layout;
PFont font;
boolean looping = true;

void setup()
{
  size( 640, 480 );
  frameRate( 15 );

  DumbParser parser = new DumbParser();
  parser.load( createInput( INPUT_FILE ) );
  stringList = parser.getWords();
  itr = stringList.iterator();

  wordMap = new HashMap();

  font = createFont( FONT_NAME, 48 );
  layout = new Layout( this, BACKGROUND );
  //layout.setAngleType( Layout.ANYWAY );
  background( BACKGROUND );
}

void draw()
{
  if ( itr.hasNext() )
  {
    String token = (String)itr.next();
    token.trim();
    token = token.toLowerCase();
    ColoredWord word = (ColoredWord)wordMap.get( token );

    // redraw the whole thing except for word
    background( BACKGROUND );
    for( Iterator witr = wordMap.values().iterator(); witr.hasNext(); )
    {
      ColoredWord w = (ColoredWord)witr.next();
      if ( w != word )
      {
        fill( w.col, w.life );
        layout.paintWord( w );
      }
    }

    if ( word == null )
    {
      word = new ColoredWord( token, 1f );
      word.font = font;
      word.col = ColorStuff.BlueIce[ (int)random(ColorStuff.BlueIce.length) ];
      wordMap.put( token, word );
      word.fontSize = (int)min( word.weight + MIN_FONTSIZE, MAX_FONTSIZE );
      layout.doLayout( word );
    }
    else
    {
      // fit word in
      word.weight += 1;
      word.fontSize = (int)min( word.weight + MIN_FONTSIZE, MAX_FONTSIZE );
      if ( layout.intersects( word ) )
        layout.doLayout( word );
    }
    
    word.life = 255;
    fill( word.col, word.life );
    layout.paintWord( word );

    // decrease life
    for( Iterator witr = wordMap.values().iterator(); witr.hasNext(); )
    {
      ColoredWord w = (ColoredWord)witr.next();
      w.life -= FADE;
      if ( w.life < 0 ) w.life = 0;
    }

    if ( TAKE_SNAPSHOTS )
      snap();
  }
  else
  {
    boolean anyleft = false;
    background( BACKGROUND );
    for( Iterator witr = wordMap.values().iterator(); witr.hasNext(); )
    {
      ColoredWord w = (ColoredWord)witr.next();
      if ( w.life > 0 )
      {
        anyleft = true;
        fill( w.col, w.life );
        layout.paintWord( w );
        w.life -= FADE;
        if ( w.life < 0 ) w.life = 0;
      }
    }
    if ( !anyleft )
    {
      println( "Done." );
      noLoop();
    }
  }
}

void snap()
{
  saveFrame( "frames/frame-######.png" );
}

void keyPressed()
{
  switch( key )
  {
  case ' ':
    looping = !looping;
    break;
  }
}

